/**业务管理流程平台版本1.0,生成时间:2017-04-13 13:16:48**/
package com.xbdun.service.bmp.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.xbdun.common.page.Pager;
import com.xbdun.dao.bmp.entity.XbdunSysOrgMenusDo;
import com.xbdun.dao.bmp.mapper.SysOrgMenusMapper;
import com.xbdun.service.bmp.biz.SysOrgMenusBiz;


/**
 * 企业被授权的菜单项
 * @ClassName XbdunSysOrgMenusServiceImpl
 * @author 张国涛
 * @2017-04-13 13:16:48
 */
@Service
@Transactional
public class SysOrgMenusBizImpl implements SysOrgMenusBiz {
	@Autowired
	private SysOrgMenusMapper sysOrgMenusMapper;
	
	Log log = LogFactory.getLog(SysOrgMenusBizImpl.class);

	/**
	 * 根据主键获取对象
	 * @return {@link XbdunSysOrgMenusDo}
	 */
	public XbdunSysOrgMenusDo getXbdunSysOrgMenusById(long id)throws RuntimeException{
		Assert.isTrue(id>0, "主键id不能小于零");
		
		return sysOrgMenusMapper.getXbdunSysOrgMenusById(id);
	}
	
	/**
	 * 新增
	 * @param xbdunSysOrgMenusDo 
	 */
	public Long addXbdunSysOrgMenus(XbdunSysOrgMenusDo xbdunSysOrgMenusDo)throws RuntimeException{
		Assert.notNull(xbdunSysOrgMenusDo,"新增xbdunSysOrgMenusDo对象不能为Null");
		
		return sysOrgMenusMapper.addXbdunSysOrgMenus(xbdunSysOrgMenusDo);
	}
	
	/**
	 * 删除数据根据主键
	 * @param idx
	 */
	public void delXbdunSysOrgMenusById(long id)throws RuntimeException{
		Assert.isTrue(id>0, "主键id不能小于零");
		
		sysOrgMenusMapper.delXbdunSysOrgMenusById(id);
	}
	
	/**
	 * 判断是否存在,不为null则是存在
	 * @param id 主键id
	 * @return Long
	 */
	public boolean existById(long id)throws RuntimeException{
		Assert.isTrue(id>0,"参数id必须大于零");
		Map<String,Object> param = new HashMap<String, Object>();
		
		//根据主键
		param.put("id",id);
		
		Long rlt = sysOrgMenusMapper.existXbdunSysOrgMenus(param);

		param.clear();
		param = null;

		return (null==rlt||rlt.longValue()==0?false:true);
	}
	
	/**
	 * 更新对象属性
	 * @param XbdunSysOrgMenusDo	更新对象
	 * @param id
	 */
	public void updateById(XbdunSysOrgMenusDo xbdunSysOrgMenusDo,long id)throws RuntimeException{
		
		Assert.isTrue(null!=xbdunSysOrgMenusDo,"更新xbdunSysOrgMenusDo对象不能为空");
		
		Map<String,Object> param = new HashMap<String,Object>();
		//TODO 自定义更新对象属性 
		/**
		 param.put("orgCode", xbdunSysOrgMenusDo.getOrgCode());
		 param.put("code", xbdunSysOrgMenusDo.getCode());

		 * 属性对应的Map的键-值
		 */
		Map<String,Object> where = new HashMap<String,Object>();
		
		where.put("id", id);
		
		sysOrgMenusMapper.updateDynXbdunSysOrgMenus(param, where);

		param.clear();
		where.clear();
		param = null;
		where = null;
	}
	
	/**
	 * 查询分页
	 * @param where			约束条件,键是对象属性其中条件都为and连接
	 * @param orderBy		排序条件
	 * @param pageNum   	当前索引页
	 * @param numPerPage	每页显示条数
	 * @return
	 * @throws RuntimeException
	 */
	public Pager<XbdunSysOrgMenusDo> queryPager(Map<String,Object> where,String orderBy,int pageIndex,int pageSize){
		log.info(where);
		//分页对象
		Pager<XbdunSysOrgMenusDo> resultPager = new Pager<XbdunSysOrgMenusDo>();
		
		resultPager.setPageSize(pageSize);
		resultPager.setPageIndex(pageIndex);
		resultPager.setSort(orderBy);
		Long resultCnt = sysOrgMenusMapper.queryPagerCount(where);
		if(null!=resultCnt&&resultCnt.longValue()==0){
			
			resultPager.setTotalCount(0);
			return resultPager;
		}
		
		//获取查询结果集List
		List<XbdunSysOrgMenusDo> list = sysOrgMenusMapper.queryPager(where, orderBy,(resultPager.getPageIndex()-1)*resultPager.getPageSize(), resultPager.getPageSize());
		
		//设置查询条数
		resultPager.setTotalCount(resultCnt.longValue());
		resultPager.setPager();
		resultPager.setData(list);
		
		return resultPager;
	}

}